package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.service.CategoryBrandService;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryBrandService categoryBrandService;
    @Override
    public List<CategoryDTO> queryCategoryByBrandId(Long bid) {
//        select category_id from tb_category_brand where brand_id = 14026
//        select * from tb_category where id in (select category_id from tb_category_brand where brand_id = 14026)
        //根据品牌id查询获取所有中间表信息
        List<CategoryBrand> categoryBrands = categoryBrandService.query().eq("brand_id", bid).list();
        //获取分类id的集合
        List<Long> ids = categoryBrands.stream().map(CategoryBrand::getCategoryId).collect(Collectors.toList());
        //获取所有商品分类
        List<Category> categories = listByIds(ids);
        return CategoryDTO.convertEntityList(categories);
    }
}
